<template>
  <div class="edit-table-column-fragment" style="position: relative;">
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      style="width: 100%;"
      label-width="100px"
      size="mini"
      label-position="right"
      @submit.native.prevent
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="字段数据表" prop="tableId">
            <el-select v-model="formData.tableId" class="input-item" size="mini" :clearable="true" placeholder="字段数据表" @change="onTableChange">
              <el-option v-for="(table, index) in tableList" :key="table.tableId" :label="table.tableName" :value="table.tableId">
                <el-row type="flex" justify="space-between" align="middle">
                  <span>{{ table.tableName }}</span>
                  <el-tag :type="table.relationType == null ? 'success' : 'parimary'" style="margin-left: 30px;" size="mini" effect="dark">
                    {{ (table.relationType == null || index === 0) ? '主表' : '一对一关联' }}
                  </el-tag>
                </el-row>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="绑定列字段" prop="columnId">
            <el-select v-model="formData.columnId" class="input-item" :clearable="true" placeholder="字段数据表" @change="onColumnChange">
              <el-option v-for="column in getTableColumnList" :key="column.columnId" :label="column.columnName" :value="column.columnId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="表格列名" prop="showName">
            <el-input v-model="formData.showName" class="input-item" :clearable="true" placeholder="表格列名" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="表格列宽" prop="columnId">
            <el-input-number v-model="formData.columnWidth" class="input-item" placeholder="表格列宽" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="支持排序" prop="columnId">
            <el-switch v-model="formData.sortable" class="input-item" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="显示顺序" prop="showOrder">
            <el-input-number v-model="formData.showOrder" class="input-item" placeholder="显示顺序" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer btnList">
      <el-button type="primary" size="mini" :plain="true" @click="onCancel(false)">取消</el-button>
      <el-button type="primary" size="mini" @click="onSubmit()">保存</el-button>
    </div>
  </div>
</template>

<script>
import { findItemFromList } from '@/utils'

export default {
  props: {
    showOrder: {
      type: Number,
      default: 0
    },
    rowData: {
      type: Object,
      default: () => {}
    },
    tableList: {
      type: Array,
      required: true
    },
    usedColumnList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: {
        columnId: undefined,
        tableId: undefined,
        showName: undefined,
        showOrder: this.showOrder,
        sortable: false,
        columnWidth: undefined
      },
      oldColumnId: undefined,
      rules: {
        showName: [
          { required: true, message: '表格列名不能为空', trigger: 'blur' }
        ],
        tableId: [
          { required: true, message: '表格列绑定数据表不能为空', trigger: 'blur' }
        ],
        columnId: [
          { required: true, message: '表格列绑定字段不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    getCurrentTable() {
      return findItemFromList(this.tableList, this.formData.tableId, 'tableId')
    },
    getTableColumnList() {
      if (this.getCurrentTable != null) {
        return this.getCurrentTable.columnList.filter(item => {
          return this.usedColumnList.indexOf(item.columnId) === -1 || this.oldColumnId === item.columnId
        })
      } else {
        return []
      }
    }
  },
  mounted() {
    if (this.rowData != null) {
      this.formData = {
        ...this.rowData
      }
      this.oldColumnId = this.formData.columnId
    }
  },
  methods: {
    onCancel(isSuccess) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess, this.formData)
      }
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.formData.table = this.getCurrentTable
        this.formData.relationId = this.getCurrentTable.relationType == null ? undefined : this.getCurrentTable.id
        this.onCancel(true)
      })
    },
    onTableChange(value) {
      this.formData.columnId = undefined
    },
    onColumnChange(value) {
      this.formData.column = findItemFromList(this.getTableColumnList, this.formData.columnId, 'columnId')
      this.formData.showName = this.formData.column ? this.formData.column.columnComment : undefined
    }
  }
}
</script>

<style  lang="scss">
  .edit-table-column-fragment {
    display: flex;

    flex-direction: column;
    margin: 10px 10px 20px 10px;
    .blueLink {
      color: #409eff;
      font-size: 14px;
      margin-left: 20px;
      cursor: pointer;
    }
    .btnList {
      margin-top:20px;
      display: flex;
      justify-content: right;
      float: right;
      .submitBtn {
        margin-right: 30px;
      }
    }
    .fromFlex {
      height: 36px;
      display: flex;
      align-items: center;
    }
    .base-info {
      display: flex;
      margin-bottom: 20px;
      .basics-info {
        width: 100%;
      }
    }
  }
</style>
